package icu.groot.alan.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.GsonBuilder;
import icu.groot.alan.hotel.entity.dao.AdminDo;
import icu.groot.alan.hotel.entity.dto.LoginAdminDto;
import icu.groot.alan.hotel.mapper.AdminMapper;
import icu.groot.alan.hotel.service.AdminService;
import icu.groot.alan.hotel.util.BizException;
import icu.groot.alan.hotel.util.GrootApi;
import icu.groot.alan.hotel.util.ResultCodeEnum;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminDo> implements AdminService {
    @Override
    public LoginAdminDto login(Integer adminCode, String adminPassword) {
        AdminDo adminDo = this.getOne(new QueryWrapper<AdminDo>().eq("admin_code", adminCode));
        if (adminDo == null){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("登录失败,用户不存在"));
        }
        if (!adminDo.getAdminPassword().equals(adminPassword)){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("登录失败, 密码错误"));
        }
        LoginAdminDto loginAdminDto = new LoginAdminDto();
        loginAdminDto.setAdminCode(adminDo.getAdminCode());
        loginAdminDto.setToken(GrootApi.Jwt.create(new GsonBuilder().create().toJson(adminDo)));
        return loginAdminDto;
    }
}
